package com.puzzlebazar.client.puzzle.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.puzzlebazar.client.util.NumberFilter;

public class RetimeDialog extends DialogBox {

	public interface Callback {
		/**
		 * Callback used when OK is pressed
		 * 
		 * @param time The time entered in the dialog box, in milliseconds
		 */
		public void ok( long time );
		/**
		 * Callback used when CANCEL is pressed
		 */
		public void cancel();
	}
	
	final TextBox minutesText = new TextBox();
	final TextBox secondsText = new TextBox();
	final TextBox millisecondsText = new TextBox();

	public RetimeDialog( final Callback callback ) throws Exception {
	
		// Set the dialog box's caption.
		setText("Enter time");

		final VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setWidth( "100%" );

		final HorizontalPanel subPanel = new HorizontalPanel();
		subPanel.setWidth( "100%" );
		
		final Label messageLabel  = new Label( "Time :" );	      
		messageLabel.setWidth( "50px" );
		minutesText.setWidth("47px");	
		secondsText.setWidth("25px");		
		millisecondsText.setWidth("33px");

		minutesText.setMaxLength(5);
		minutesText.addKeyPressHandler( new NumberFilter() );
		secondsText.setMaxLength(2);
		secondsText.addKeyPressHandler( new NumberFilter() );
		millisecondsText.setMaxLength(3);
		millisecondsText.addKeyPressHandler( new NumberFilter() );
		
		subPanel.add( messageLabel );
		subPanel.add( minutesText );
		subPanel.add( new Label(":") );
		subPanel.add( secondsText );
		subPanel.add( new Label(".") );
		subPanel.add( millisecondsText );
		
		final HorizontalPanel buttonPanel = new HorizontalPanel();

		final Button ok = new Button("OK");
		ok.setWidth( "75px" );
		ok.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callback.ok( getTime() );
				RetimeDialog.this.hide();
			}

		});
		
		final Button cancel = new Button("Cancel");
		cancel.setWidth( "75px" );
		cancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callback.cancel();
				RetimeDialog.this.hide();
			}
		});
		
		buttonPanel.add( ok );
		buttonPanel.setCellWidth( ok, "80px" );
		buttonPanel.add( cancel );
		buttonPanel.setCellWidth( cancel, "80px" );
		
		mainPanel.add( subPanel );
		mainPanel.add( buttonPanel );
		setWidget(mainPanel);
		setModal(true);

		setAnimationEnabled(true);

		DeferredCommand.addCommand( new Command() {
			@Override
			public void execute() {
				minutesText.setFocus( true );
				minutesText.selectAll();
			}			
		});
		
	}

	/**
	 * Sets the time to display in the edit fields
	 * 
	 * @param message The message to display
	 */
	public void setTime(long time) {
		String ms = String.valueOf(time % 1000);
		while( ms.length() < 3 )
			ms = "0" + ms;
		millisecondsText.setText( ms );
		time /= 1000;
		String sec = String.valueOf(time % 60);
		while( sec.length() < 2 )
			sec = "0" + sec;
		secondsText.setText( sec );
		time /= 60;
		String min = String.valueOf(time);
		while( min.length() < 5 )
			min = "0" + min;
		minutesText.setText( min );
	}


	/**
	 * Get the time displayed in the edit fields
	 * 
	 * @return The time displayed
	 */
	public long getTime() {
		long result = Integer.valueOf( minutesText.getText() ) * 60000 +
			Integer.valueOf( secondsText.getText() ) * 1000 +
			Integer.valueOf( millisecondsText.getText() );
	
		if( result <= 0 )
			result = 1;
		
		return result;
	}
}
